package com.shankungfu.main;

import android.app.Notification;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.baidu.android.pushservice.CustomPushNotificationBuilder;
import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.loopj.android.http.PersistentCookieStore;
import com.shankungfu.MainApplication;
import com.shankungfu.R;
import com.shankungfu.fragment.MyMenuFragment;
import com.shankungfu.fragment.PickItemFragment;
import com.shankungfu.util.ToastUtil;
import com.shankungfu.util.Utils;
import com.tendcloud.tenddata.TCAgent;

public class MainActivity extends BaseActivity  implements OnClickListener {
	
	/** 两次返回键时间间隔 */
	private static final int DoubleBackPressedInterval = 1000;
	/** 上一次返回键按下时间 */
	private long lastBackPressedTime = 0;
	private ImageView openMenu;
	private ImageView refreshBtn;
	private Fragment mContent;
	
	public MainActivity() {
		super(R.string.app_name);
	}
	
	public MainActivity(int titleRes) {
		super(R.string.app_name);
	}
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContent = new PickItemFragment();
		TCAgent.init(this);
		// 以apikey的方式登录，一般放在主Activity的onCreate中
				PushManager.startWork(getApplicationContext(),
						PushConstants.LOGIN_TYPE_API_KEY, 
						Utils.getMetaValue(MainActivity.this, "api_key"));
		
		// set the Above View
		setContentView(R.layout.content_frame);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.content_frame, mContent).commit();

		// set the Behind View
		setBehindContentView(R.layout.menu_frame);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.menu_frame, new MyMenuFragment()).commit();

		openMenu = (ImageView) findViewById(R.id.head_goback);
		refreshBtn = (ImageView) findViewById(R.id.head_refresh);
		
		openMenu.setOnClickListener(this);
		refreshBtn.setOnClickListener(this);
		
		
		CustomPushNotificationBuilder cBuilder = new CustomPushNotificationBuilder(
        		getApplicationContext(),
        		R.layout.notification_custom_builder,
        		R.id.notification_icon,
        		R.id.notification_title,
        		R.id.notification_text);
        cBuilder.setNotificationFlags(Notification.FLAG_AUTO_CANCEL);
        cBuilder.setNotificationDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
        cBuilder.setStatusbarIcon(this.getApplicationInfo().icon);
        cBuilder.setLayoutDrawable(R.drawable.default_icon);
		PushManager.setNotificationBuilder(this, 1, cBuilder);
		
		// customize the SlidingMenu
//		getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
//		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		
		
//		getSupportActionBar().setCustomView(R.layout.head_layout);
//		TestUtils.logE("d");
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		TCAgent.onResume(this);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		TCAgent.onPause(this);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		getSupportFragmentManager().putFragment(outState, "mContent", mContent);
	}
	
	public void switchContent(Fragment fragment) {
		mContent = fragment;
		getSupportFragmentManager().beginTransaction()
			.replace(R.id.content_frame, fragment).commit();
		getSlidingMenu().showContent();
	}

	public void showMenuLayout() {
		toggle();
	}
	
	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.head_goback: //打开菜单
			toggle();
			break;
		}
		
	}

	@Override
	public void onBackPressed() {
		long currentTimeMillis = System.currentTimeMillis();
		if (currentTimeMillis - lastBackPressedTime > DoubleBackPressedInterval) {
			ToastUtil.showMessageText(this, "再次点击返回键退出程序");
			lastBackPressedTime = currentTimeMillis;
		} else {
			super.onBackPressed();
			PersistentCookieStore cookieStore = new PersistentCookieStore(MainApplication.context);
			cookieStore.clear();
		}
	}
}
